package com.entor.hqj.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.entor.hqj.util.HttpPostUtils;

public class RegisterActivity extends Activity implements View.OnClickListener{

    /**
     * 左边图片按钮
     */
    private ImageView title_left_imageview;
    /**
     * 左边文字按钮
     */
    private TextView title_left_textview;
    /**
     * 中间显示文字
     */
    private TextView title_middle_textview;
    /**
     * 右边文字按钮
     */
    private TextView title_right_textview;
    /**
     * 右边图片按钮
     */
    private ImageView title_right_imageview;

    private EditText loginname;
    private EditText username;
    private EditText password;
    private EditText comPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
        setIsVisible();
        setListener();
    }

    /**
     * 初始化控件
     */
    private void initView(){
        title_left_imageview = (ImageView) findViewById(R.id.title_left_imageview);
        title_left_textview = (TextView) findViewById(R.id.title_left_textview);
        title_middle_textview = (TextView) findViewById(R.id.title_middle_textview);
        title_right_textview = (TextView) findViewById(R.id.title_right_textview);
        title_right_imageview = (ImageView) findViewById(R.id.title_right_imageview);

        loginname = (EditText)findViewById(R.id.et_loginName_reg);
        username = (EditText)findViewById(R.id.et_userName_reg);
        password = (EditText)findViewById(R.id.et_password_reg);
        comPassword = (EditText)findViewById(R.id.et_password_com);
    }

    /**
     * 设置某个是否显示
     */
    private void setIsVisible(){
        /**
         * 1.因为在配置文件中设置的是默认全部隐藏
         * 2.因此在这里直接设置显示，然后在进行配置即可
         * 3.中间的TextView没有设置隐藏，因此这里不用设置（可根据自己的需要进行配置）
         */
        //显示返回图片
        title_left_imageview.setVisibility(View.VISIBLE);
        title_left_imageview.setImageResource(R.mipmap.layout_top_back);

        //设置中间文字
        title_middle_textview.setText("用户注册");

        //显示右侧文字
        title_right_textview.setVisibility(View.VISIBLE);
        title_right_textview.setText("确定");
    }

    /**
     * 设置点击事件
     */
    private void setListener(){
        //设置左侧返回图片事件
        title_left_imageview.setOnClickListener(this);

        //设置右侧确定文本事件
        title_right_textview.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            /**
             * 根据不同按钮点击进行相应的操作
             * 1.这里也可以根据上面设定的显示进行事件的监听
             */
            //监听左侧返回图片的事件
            case R.id.title_left_imageview:
                System.out.println("点击返回图片");
                //要返回上一个activity，只需要结束当前的activity
                finish();
                break;
            //监听右侧确定文本的事件
            case R.id.title_right_textview:
                System.out.println("点击确定按钮执行注册方法");
                String loginname = this.loginname.getText().toString().trim();
                String name = this.username.getText().toString().trim();
                String password = this.password.getText().toString().trim();
                String comPassword = this.comPassword.getText().toString().trim();
                if(!password.equals(comPassword)){
                    Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    break;
                }else if(loginname==null||loginname==""){
                    Toast.makeText(RegisterActivity.this, "登录名不能为空", Toast.LENGTH_SHORT).show();
                    break;
                }else if(name==null||name==""){
                    Toast.makeText(RegisterActivity.this, "名称不能为空", Toast.LENGTH_SHORT).show();
                    break;
                }

                final String reg = "loginname="+loginname+"&password="+password+"&name="+name+"&organizationId=1";
                new Thread(){
                    public void run(){
                        String responce =
                                HttpPostUtils.doPostRequest("user/addUserByMobile",reg);
                        Message msg = Message.obtain();
                        msg.obj = responce;
                        handler.sendMessage(msg);
                    }
                }.start();
                break;
        }

    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message){
            String result = (String)message.obj;
            JSONObject resultJson =
                    JSON.parseObject(result);
            if(resultJson!=null) {
                String msg = resultJson.getString("msg");
                boolean sus = resultJson.getBoolean("success");
                Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
                if(sus){
                    Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                    startActivity(intent);
                }
            }
        }
    };
}
